﻿// utf8模板
#include "OiPCH.hpp"
#include "OiResultWidget.hpp"


namespace Oi
{
    namespace PLScannerDll
    {
        class ResultWidgetImpl :public Impl<ResultWidget>
        {
        public:
            void init(const QString& title);
            void exit();

        public:
            QGridLayout* _lay = nullptr;
            QLabel* _titleLab = nullptr;
            QLabel* _stdLab = nullptr;
            QLabel* _msrLab = nullptr;
            QLabel* _disLab = nullptr;
            QLabel* _rateLab = nullptr;
            QLabel* _cpLab = nullptr;
            QLabel* _cpkLab = nullptr;
        };

        void ResultWidgetImpl::init(const QString& title)
        {
            OI_Q(ResultWidget);
            _lay = new QGridLayout(q);
            q->setLayout(_lay);

            _titleLab = new QLabel(title, q);
            _titleLab->setAlignment(Qt::AlignCenter);
            QFont ft = _titleLab->font();
            ft.setPointSize(30);
            ft.setBold(true);
            _titleLab->setFont(ft);
            _lay->addWidget(_titleLab, 0, 0, 1, 3);

            QLabel* labStd = new QLabel("标准值", q);
            labStd->setAlignment(Qt::AlignCenter);
            QLabel* labMsr = new QLabel("测量值", q);
            labMsr->setAlignment(Qt::AlignCenter);
            QLabel* labDis = new QLabel("差值", q);
            labDis->setAlignment(Qt::AlignCenter);
            _lay->addWidget(labStd, 1, 0);
            _lay->addWidget(labMsr, 1, 1);
            _lay->addWidget(labDis, 1, 2);

            _stdLab = new QLabel("0", q);
            _stdLab->setAlignment(Qt::AlignCenter);
            _msrLab = new QLabel("0", q);
            _msrLab->setAlignment(Qt::AlignCenter);
            _disLab = new QLabel("0", q);
            _disLab->setAlignment(Qt::AlignCenter);
            _lay->addWidget(_stdLab, 2, 0);
            _lay->addWidget(_msrLab, 2, 1);
            _lay->addWidget(_disLab, 2, 2);
            
            QLabel* labRate = new QLabel("合格率", q);
            labRate->setAlignment(Qt::AlignCenter);
            QLabel* labCP = new QLabel("Cp", q);
            labCP->setAlignment(Qt::AlignCenter);
            QLabel* labCPK = new QLabel("Cpk", q);
            labCPK->setAlignment(Qt::AlignCenter);
            _lay->addWidget(labRate, 3, 0);
            _lay->addWidget(labCP, 3, 1);
            _lay->addWidget(labCPK, 3, 2);

            _rateLab = new QLabel("0", q);
            _rateLab->setAlignment(Qt::AlignCenter);
            _cpLab = new QLabel("0", q);
            _cpLab->setAlignment(Qt::AlignCenter);
            _cpkLab = new QLabel("0", q);
            _cpkLab->setAlignment(Qt::AlignCenter);
            _lay->addWidget(_rateLab, 4, 0);
            _lay->addWidget(_cpLab, 4, 1);
            _lay->addWidget(_cpkLab, 4, 2);

            labRate->setVisible(false);
            labCP->setVisible(false);
            labCPK->setVisible(false);
            _rateLab->setVisible(false);
            _cpLab->setVisible(false);
            _cpkLab->setVisible(false);

            ft.setPointSize(15);
            ft.setBold(false);
            labStd->setFont(ft);
            labMsr->setFont(ft);
            labDis->setFont(ft);
            _stdLab->setFont(ft);
            _msrLab->setFont(ft);
            _disLab->setFont(ft);
            labRate->setFont(ft);
            labCP->setFont(ft);
            labCPK->setFont(ft);
            _rateLab->setFont(ft);
            _cpLab->setFont(ft);
            _cpkLab->setFont(ft);

            //q->setMaximumSize(240, 200);
        }

        void ResultWidgetImpl::exit()
        {

        }


        ResultWidget::ResultWidget(const QString& title, QWidget* parent)
            : QWidget(parent)
        {
            OI_I(ResultWidget)->init(title);
        }

        ResultWidget::~ResultWidget()
        {
            OI_E(ResultWidget)->exit();
        }

        QString ResultWidget::title() const
        {
            OI_C(ResultWidget);
            return d->_titleLab->text();
        }

        void ResultWidget::setValue(double msrVal, double stdVal)
        {
            OI_F(ResultWidget);
            QString txt = QString::number(msrVal, 'f', 2);
            d->_msrLab->setText(txt);
            txt = QString::number(stdVal, 'f', 2);
            d->_stdLab->setText(txt);
            double disVal = msrVal - stdVal;
            txt = QString::number(disVal, 'f', 2);
            d->_disLab->setText(txt);
        }

        void ResultWidget::setRateCPCPK(double rate, double cp, double cpk)
        {
            OI_F(ResultWidget);
            QString txt = QString::number(rate, 'f', 2) + "%";
            d->_rateLab->setText(txt);
            txt = QString::number(cp, 'f', 2);
            d->_cpLab->setText(txt);
            txt = QString::number(cpk, 'f', 2);
            d->_cpkLab->setText(txt);
        }
    }
}


